package com.qijian.web.model.report;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Data
@NoArgsConstructor
public class SaleCapacityReport {
    private String yearMonth;

    private Integer month;

    private String workType;

    private Long actualTime;

    private Long totalTime;

    private LocalDate date;

    private Long userDeviceCount;

    private Double workTime;

    /*----------------------------------------
    以下走计算逻辑
    ----------------------------------------*/

    //需要人员/设备数量
    //当月 实际工种工作时间/工作每日作业时间
    // 100/8
    private Double needUserDeviceCount;

    //多余/缺少人员数量
    //用已有的人-（需要人员/设备数量）
    private Double diffUserDeviceCount;


    public SaleCapacityReport(LocalDate date, String workType, Long actualTime) {
        this.setDate(date);
        this.workType = workType;
        this.actualTime = actualTime;
    }

    public void setDate(LocalDate date) {
        this.date = date;
        if (date != null) {
            this.yearMonth = date.format(DateTimeFormatter.ofPattern("yyyy-MM"));
            this.month = date.getMonthValue();
        }
    }

    /**
     * 累计合并 (避免修改原对象。返回一个新的对象)
     *
     * @return 合并后的结果
     */
    public SaleCapacityReport addUp(SaleCapacityReport up) {
        SaleCapacityReport result = new SaleCapacityReport();
        result.setDate(up.getDate());
        result.setWorkType(up.getWorkType());
        result.setActualTime(this.getActualTime() + up.getActualTime());
        result.setTotalTime(up.getTotalTime());
        //统计到月,所以合并时需要累计
        result.setUserDeviceCount(up.getUserDeviceCount());
        result.setWorkTime(up.getWorkTime());
        return result;
    }

    public static SaleCapacityReport init() {
        SaleCapacityReport entity = new SaleCapacityReport();
        entity.setActualTime(0L);
        entity.setTotalTime(0L);
        entity.setUserDeviceCount(0L);
        entity.setWorkTime(0D);
        return entity;
    }

    public void cal() {
        if (this.actualTime != null && this.actualTime != 0
                && this.workTime != null && this.workTime != 0) {
            this.needUserDeviceCount = new BigDecimal(this.actualTime / this.workTime).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
            this.diffUserDeviceCount = (this.userDeviceCount == null ? 0 : this.userDeviceCount) - this.needUserDeviceCount;
        } else {
            this.needUserDeviceCount = 0D;
            this.diffUserDeviceCount = 0D;
        }
    }

}
